package chances.epg.ui.taglib.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;

import com.google.common.base.Strings;


import chances.epg.commons.utils.PathUtils;

@SuppressWarnings("serial")
public class ImgTag extends EpgRectTag {
	private String context;
	private String src;
	private String var;
	private String image;
	private String src1;
	private String src2;

	private String defaultImage;
	private int width;
	private int height;

	@Override
	public void doAfterStart() throws JspException {

		if (Strings.isNullOrEmpty(context)) {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			context = request.getContextPath();
		}
		this.image = this.getImageSrc();

		if (StringUtils.isEmpty(image)) {
			this.image = getDefaultImage();
		} else {
			this.image = PathUtils.join(context, this.image );
		}
		if (StringUtils.isNotEmpty(var)) {
			pageContext.setAttribute(var, this.image);
			this.setDisplay(false);
		}
		super.doAfterStart();
	}





	protected String getImageSrc() {
		if (StringUtils.isNotEmpty(this.src)) {
			return this.src;
		} else if (StringUtils.isNotEmpty(this.src1)) {
			return this.src1;
		} else if (StringUtils.isNotEmpty(this.src2)) {
			return this.src2;
		}
		return null;
	}

	@Override
	public void permutate() {
		super.permutate();
		if (this.width == 0) {
			this.width = this.rect.getWidth();
		}
		if (this.height == 0) {
			this.height = this.rect.getHeight();
		}

	}

	public String getDefaultImage() {
		String name = defaultImage;
		if (StringUtils.isEmpty(name)) {
			name =this.epgPageContext.getDefaultImage();
		}

		if (!name.startsWith("http://")) {
			String path = pageContext.getServletContext().getContextPath();
			return PathUtils.join(path, name);
		}
		return name;
	}

	@Override
	public void release() {
		this.src = null;
		this.context = null;
		this.image = null;
		this.defaultImage = null;
		this.width = 0;
		this.height = 0;
		this.src = null;
		super.release();
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public void setDefaultImage(String defaultImage) {
		this.defaultImage = defaultImage;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getSrc1() {
		return src1;
	}

	public String getSrc2() {
		return src2;
	}

	public void setSrc1(String src1) {
		this.src1 = src1;
	}

	public void setSrc2(String src2) {
		this.src2 = src2;
	}
}
